package cn.z2huo.demo.spring.webmvc.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>{@link RequestMapping} 注解的属性使用示例。
 * 例如 params、headers、consumes、produces 等属性的使用示例。
 *
 * @author z2huo
 */
@Slf4j
@RestController
public class RequestMappingAttrTestController {

    @RequestMapping(value = "/requestMapping/test",
            method = RequestMethod.GET,
            // 要求入参中的 name 和 age 必须与注解中的 params 中设置的一致
            // 否则会报错：Resolved [org.springframework.web.bind.UnsatisfiedServletRequestParameterException:
            // Parameter conditions "name=zyz, age=18" not met for actual request parameters: name={aaa}, age={18}]
            params = {"name=zyz", "age=18"})
    public void test(@RequestParam String name, @RequestParam int age) {
        log.info("test, name is {}, age is {}", name, age);
    }

    // 两个相同的 value/path 的 @RequestMapping 注解，但是 params 不同，是可行的
    @RequestMapping(value = "/requestMapping/test",
            method = RequestMethod.GET,
            params = {"name=zzz", "age=27"})
    public void test2(@RequestParam String name, @RequestParam int age) {
        log.info("test2, name is {}, age is {}", name, age);
    }

    // 两个什么参数都相同的 @RequestMapping 注解，启动时会报错
    /*
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Ambiguous mapping. Cannot map 'requestMappingAttrTestController' method
    cn.z2huo.demo.spring.webmvc.controller.RequestMappingAttrTestController#test3(String, int)
    to {GET [/requestMapping/test], params [name=zzz && age=27]}: There is already 'requestMappingAttrTestController' bean method
     */
    @RequestMapping(value = "/requestMapping/test",
            method = RequestMethod.GET,
//            params = {"name=zzz", "age=27"}
            params = {"name=zzz", "age=28"}
    )
    public void test3(@RequestParam String name, @RequestParam int age) {
        log.info("test3, name is {}, age is {}", name, age);
    }

}
